<?php
declare(strict_types=1);

namespace App\AdminApi\Module\Admin\Service;

use App\AdminApi\Module\Admin\Enums\RoleEnums;
use App\Common\Model\Admin\RoleModel;
use Hyperf\Database\Model\Builder;
use Zms\Basics\BasicsServe;
use Zms\Unit\Is;

class RoleService extends BasicsServe
{
    /**
     * @return RoleModel::class
     */
    public static function model(): string
    {
        return RoleModel::class;
    }


    /**
     * 行为搜索器
     * @param Builder $query
     * @param $keyword
     * @return void
     */
    public function searchName(Builder $query, $keyword): void
    {
        if (Is::notEmpty($keyword)) {
            if (is_string($keyword)) {
                $query->where('name', 'LIKE', "%$keyword%");
            }
        }
    }


    /**
     * 获取全部管理员角色
     */
    public function allRole(): array
    {
        return $this->search()->orderByRaw('id DESC')->select(['id', 'name', 'status'])->get()->toArray();
    }


    /**
     * 角色表单数据
     * @return array
     */
    public function roleFormData(): array
    {
        $data = [];
        foreach ($this->allRole() as $item) {
            $data[] = [
                'value' => (string)$item['id'],
                'label' => $item['name'],
                'disabled' => $item['status'] === RoleEnums::STATUS_DISABLED
            ];
        }
        return $data;
    }
}